package com.nondo.dean.order.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.order.domain.Bank;
import com.nondo.dean.order.model.param.PlatformOrderParam;
import com.nondo.dean.order.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-06-159:30
 * @Version
 */
@RestController("apiAdminBank")
@RequestMapping("/{v}/admin/bank")
public class BankController {

    @Autowired
    BankService bankService;


    /**
     * 新增
     * @param request
     * @param response
     * @param bank
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Bank> createBank(HttpServletRequest request, HttpServletResponse response,
                                 @Validated @RequestBody Bank bank, BindingResult result) {
        View<Bank> view = new View<Bank>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.bankService.add(bank);
        }
        return view;
    }

    /**
     * 查询单个详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Bank> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Bank> view = new View<Bank>(20000, "获取数据成功");

        Bank bank = null;
        if (id != null) {
            bank = bankService.select(id);
        } else {
            bank = new Bank();
        }

        view.setData(bank);
        return view;
    }

    /**
     * 删除银行
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Bank bank = bankService.select(id);
        bank.setDelFlag(Bank.DEL_FLAG_DELETE);
        this.bankService.update(bank);

        return view;
    }

    /**
     * 修改银行信息
     * @param request
     * @param response
     * @param bank
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Bank> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Bank bank, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.bankService.update(bank);
        }

        return view;
    }

    /**
     * 获取列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Bank>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Bank> query) {
        View<Page<Bank>> view = new View<Page<Bank>>(20000, "获取数据成功");

        Bank bank = query.getData();
        if(bank== null){
            bank=new Bank();
        }

        Page<Bank> page = new Page<Bank>(query.getPage(), query.getLimit(), bankService.count(bank));

        List bankList = bankService.getList(query.getOffset(), query.getLimit(), bank);

        page.setContent(bankList);

        view.setData(page);

        return view;
    }
}
